package com.example.jsonquynh;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

public class DetailPlace extends SherlockActivity {
	private Place place;
	private TextView tvAddressDetail;
	private TextView tvDescription;
	com.actionbarsherlock.view.MenuItem favorite, showmap;

	FavoriteManager favoriteManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailpage);
		tvAddressDetail = (TextView) findViewById(R.id.tvAddressDetail);
		tvDescription = (TextView) findViewById(R.id.tvDescription);
		Intent i = getIntent();
		place = i.getParcelableExtra("place");
		setTitle(place.getTitle());
		tvAddressDetail.setText(place.getAddress());
		tvDescription.setText(place.getDescription());
		new Thread(new Runnable() {

			@Override
			public void run() {
				JSONParser pa = new JSONParser();
				JSONObject json = pa.getJSONFromUrl(MainActivity.URL_SERVICE
						+ "json/detail.php?id=" + place.getID());
				try {
					JSONArray places = json.getJSONArray("place");
					HashMap<String, String> map = null;
					JSONObject ob = places.getJSONObject(0);
					place.setTitle(new String(ob.getString(Place.JSON_NAME)
							.getBytes(), "utf-8"));
					place.setAddress(new String(ob
							.getString(Place.JSON_ADDRESS).getBytes(), "utf-8"));
					place.setPosition(new String(ob.getString(
							Place.JSON_POSITION).getBytes(), "utf-8"));
					place.setDescription(new String(ob.getString(
							Place.JSON_DESCRIPTION).getBytes(), "utf-8"));

					final ArrayList<String> arimage = new ArrayList<String>();
					JSONArray images = ob.getJSONArray(Place.JSON_THUMB);
					place.setThumbnai(images.getString(0));
					String strImages = "";
					images = ob.getJSONArray(Place.JSON_IMAGES);
					for (int j = 0; j < images.length(); j++) {
						arimage.add(images.getString(j));
						place.addImage(images.getString(j));
					}
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							setTitle(place.getTitle());
							tvAddressDetail.setText(place.getAddress());
							tvDescription.setText(place.getDescription());
							Gallery g = (Gallery) findViewById(R.id.gallery);
							g.setAdapter(new GalleryAdapter(DetailPlace.this,
									arimage));

						}
					});

				} catch (JSONException e) {
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}).start();

	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		showmap = menu.add("Map");
		favorite = menu.add("Favorite");

		updateFavorite();
		showmap.setShowAsAction(com.actionbarsherlock.view.MenuItem.SHOW_AS_ACTION_IF_ROOM);
		return true;
	}

	@Override
	protected void onDestroy() {
		favoriteManager.close();
		System.out.println("close db");
		super.onDestroy();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item == favorite) {
			if (!favoriteManager.isExitID(place.getID())) {
				favoriteManager.insertPlace(place);
			} else {
				favoriteManager.deletePlaceID(place.getID());
			}
			updateFavorite();

		} else if (item == showmap) {
			String uri = "geo:0,0?q=" + place.getPosition() + "?z=12 ("
					+ place.getTitle() + ")";
			System.out.println(uri);
			startActivity(new Intent(android.content.Intent.ACTION_VIEW,
					Uri.parse(uri)));
		}
		return true;
	}

	public void updateFavorite() {
		if (favoriteManager == null) {
			favoriteManager = new FavoriteManager(this);
		}
		if (!favoriteManager.isExitID(place.getID())) {
			favorite.setIcon(R.drawable.star).setShowAsAction(
					com.actionbarsherlock.view.MenuItem.SHOW_AS_ACTION_IF_ROOM);
		} else {
			favorite.setIcon(R.drawable.star_enb).setShowAsAction(
					com.actionbarsherlock.view.MenuItem.SHOW_AS_ACTION_IF_ROOM);
		}
	}

	public class GalleryAdapter extends BaseAdapter {
		LayoutInflater inflater;
		private final Context mContext;
		private ArrayList<String> images;

		public GalleryAdapter(Context c, ArrayList<String> images) {
			mContext = c;
			this.images = images;
			inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return images.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View rowView = convertView;
			if (rowView == null) {
				rowView = inflater.inflate(R.layout.list_row_gallery, null);
				ViewHolder holder = new ViewHolder();
				holder.imageView = (ImageView) rowView
						.findViewById(R.id.list_image);
				rowView.setTag(holder);

			}
			ViewHolder holder = (ViewHolder) rowView.getTag();
			String img = MainActivity.URL_SERVICE + "anh/"
					+ images.get(position);
			UrlImageViewHelper.setUrlDrawable(holder.imageView, img);
			
			return rowView;

		}

	}

	static class ViewHolder {
		ImageView imageView;
	}
}
